package cn.com.qiter.staservice.controller;


import cn.com.qiter.common.Result;
import cn.com.qiter.staservice.entity.StatisticsUserdaily;
import cn.com.qiter.staservice.entity.vo.StatisticsUserDailyQuery;
import cn.com.qiter.staservice.service.StatisticsUserdailyService;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 统计日数据 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-05-13
 */
@RestController
@RequestMapping("/staservice/userdaily")
public class StatisticsUserdailyController {

    @Autowired
    StatisticsUserdailyService service;

    //1 查询讲师表所有数据
    //rest风格
    @ApiOperation(value = "查询所有的数据")
    @GetMapping("findAll")
    public Result findAll() {
        //调用service的方法实现查询所有的操作
        List<StatisticsUserdaily> list = service.list(null);
        return Result.ok().data("items",list);
    }

    //2 逻辑删除讲师的方法
    @ApiOperation(value = "逻辑删除")
    @DeleteMapping("remove/{id}")
    public Result remove(@ApiParam(name = "id", value = "ID", required = true)
                         @PathVariable String id) {
        boolean flag = service.removeById(id);
        if(flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    //3 分页查询讲师的方法
    //current 当前页
    //limit 每页记录数
    @ApiOperation(value = "分布查询")
    @GetMapping("page/{current}/{limit}")
    public Result page(@PathVariable long current,
                       @PathVariable long limit) {
        //创建page对象
        Page<StatisticsUserdaily> pageTeacher = new Page<>(current,limit);

        //调用方法实现分页
        //调用方法时候，底层封装，把分页所有数据封装到pageTeacher对象里面
        service.page(pageTeacher,null);

        long total = pageTeacher.getTotal();//总记录数
        List<StatisticsUserdaily> records = pageTeacher.getRecords(); //数据list集合

        return Result.ok().data("total",total).data("rows",records);
    }

    //4 条件查询带分页的方法
    @ApiOperation(value = "分布查询")
    @PostMapping("page/condition/{current}/{limit}")
    public Result pageCondition(@PathVariable long current,@PathVariable long limit,
                                @RequestBody(required = false) StatisticsUserDailyQuery teacherQuery) {
        //创建page对象
        Page<StatisticsUserdaily> pageTeacher = new Page<>(current,limit);

        //构建条件
        QueryWrapper<StatisticsUserdaily> wrapper = new QueryWrapper<>();
        // 多条件组合查询
        // mybatis学过 动态sql
        String dateCalculated = teacherQuery.getDateCalculated();
        String nickname = teacherQuery.getNickname();
        String userId = teacherQuery.getUserId();
        String workType = teacherQuery.getWorkType();
        String prj = teacherQuery.getProjectId();
        if(!StringUtils.isEmpty(dateCalculated)) {
            //构建条件
            wrapper.like("date_calculated",dateCalculated);
        }
        if(!StringUtils.isEmpty(workType)) {
            wrapper.eq("work_type",workType);
        }
        if(!StringUtils.isEmpty(nickname)) {
            wrapper.eq("nickname",nickname);
        }
        if(!StringUtils.isEmpty(userId)) {
            wrapper.eq("user_id",userId);
        }
        if(!StringUtils.isEmpty(prj)) {
            wrapper.eq("project_id",prj);
        }
        //排序
        wrapper.orderByDesc("gmt_create");

        //调用方法实现条件查询分页
        service.page(pageTeacher,wrapper);

        long total = pageTeacher.getTotal();//总记录数
        List<StatisticsUserdaily> records = pageTeacher.getRecords(); //数据list集合
        return Result.ok().data("total",total).data("rows",records);
    }

    //添加讲师接口的方法
    @ApiOperation(value = "新增用户级统计")
    @PostMapping("save")
    public Result save(@RequestBody StatisticsUserdaily eduTeacher) {
        boolean save = service.save(eduTeacher);
        if(save) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    //根据讲师id进行查询
    @ApiOperation(value = "根据ID查询一条记录")
    @GetMapping("getOne/{id}")
    public Result getOne(@PathVariable String id) {
        StatisticsUserdaily eduTeacher = service.getById(id);
        return Result.ok().data("teacher",eduTeacher);
    }

    //讲师修改功能
    @ApiOperation(value = "更新用户级统计")
    @PostMapping("update")
    public Result update(@RequestBody StatisticsUserdaily eduTeacher) {
        boolean flag = service.updateById(eduTeacher);
        if(flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }


    /**
     *
     * @param
     * @return
     */
    @ApiOperation(value = "统计整个表数量")
    @GetMapping("countAll")
    public Result countAll() {
        Integer count = service.getBaseMapper().selectCount(null);
        return Result.ok().data("count",count);
    }

    //图表显示，返回两部分数据，日期json数组，数量json数组
    @GetMapping("showMonthData/{userId}/{projectId}/{type}/{year}/{month}")
    public Result showMonthData(@PathVariable String userId, @PathVariable String projectId, @PathVariable String type, @PathVariable String year,
                                @PathVariable String month) {
        DateTime st = new DateTime();
        st.setField(DateField.MONTH,Integer.valueOf(month)-1);
        st.setField(DateField.YEAR,Integer.valueOf(year));
        st.setField(DateField.DAY_OF_MONTH,Integer.valueOf(1));
        DateTime et = new DateTime();
        et.setField(DateField.MONTH,Integer.valueOf(month)-1);
        et.setField(DateField.YEAR,Integer.valueOf(year));
        et.setField(DateField.DAY_OF_MONTH,Integer.valueOf(31));
        String stime = st.toString(DatePattern.NORM_DATE_PATTERN);
        String etime = et.toString(DatePattern.NORM_DATE_PATTERN);
        Result result = service.getShowData(projectId,userId,type,stime,etime);
        return result;
    }

}

